﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace YeaTrip.Web.Common
{
    public class Error
    {
        /// <summary>
        /// 获取Controller.ModelState中的所有错误信息 利用MVC的实体验证框架，AJAX提交，返回错误信息。
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        public static string GetModelStateErrors(ModelStateDictionary dic, char split = '；')
        {
            if (dic == null)
            {
                return "";
            }
            StringBuilder sbErrors = new StringBuilder();
            foreach (var item in dic.Values)
            {
                if (item.Errors.Count > 0)
                {
                    for (int i = item.Errors.Count - 1; i >= 0; i--)
                    {
                        sbErrors.Append(item.Errors[i].ErrorMessage);
                        sbErrors.Append(split);
                    }
                }
            }
            return sbErrors.ToString();
        }
    }
}